package lianxi1;

import java.util.ArrayList;
import java.util.Scanner;

public class commodityOperator {
    public commodityOperator() {}
    private ArrayList<commodity> commodityList = new ArrayList<commodity>();
    public void addCommodity(){
        commodity c =new commodity();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商品名称：");
        String name = sc.next();
        System.out.println("请输入商品价格：");
        double price=sc.nextDouble();
        System.out.println("请输入商品数量：");
        int quantity=sc.nextInt();
        c.setName(name);
        c.setPrice(price);
        c.setQuantity(quantity);
        commodityList.add(c);
        System.out.println("添加成功！");
    }
    public void showCommodityList(){
        if(commodityList.size()==0){
            System.out.println("商品列表为空！");
            return;
        }
        System.out.println("商品列表：");
        for(commodity c:commodityList){
            c.show();
        }
    }
    public void menu(){
        Scanner sc = new Scanner(System.in);
        int choice;
        do{
            System.out.println("1.添加商品");
            System.out.println("2.显示商品列表");
            System.out.println("3.退出");
            System.out.print("请选择：");
            choice = sc.nextInt();
            switch(choice){
                case 1:
                    addCommodity();
                    break;
                case 2:
                    showCommodityList();
                    break;
                case 3:
                    System.out.println("退出成功！");
                    break;
                default:
                    System.out.println("输入错误，请重新输入！");
                    break;
            }
        }while(choice!=3);


    }
}

